import os from 'node:os'
import { AutoImport, Components, Inspect, NaiveUiResolver, cp, createExternal, createVitePlugins, unocss, usePHP, visualizer, vueDevTools } from '@na/vite'

import { setupLaravel } from './laravel'

function phpPlugin(isBuild: boolean) {
  if (isBuild) {
    return {}
  }
  let binary = '/www/server/php/82/bin/php'
  // window=Windows_NT mac=Darwin  linux=Linux
  if (os.type() === 'Windows_NT') {
    // windows平台
    binary = 'E:\\phpEnv\\php\\php-8.2\\php.exe'
  }
  return usePHP({
    binary,
    entry: ['index.php'],
  })
}

const copyFile = (src: string, rename?: string, options?: Record<string, any>) => {
  return { src: `./node_modules/${src}`, dest: 'public/build/libs', rename, ...options }
}

export default async function createPlugins(configEnv: any) {
  const isBuild = configEnv.command === 'build'
  const plugins = createVitePlugins([
    vueDevTools(),
    Inspect({
      // build: true,
      // outputDir: '.vite-inspect'
    }),
    AutoImport({
      dts: 'src/typings/auto-imports.d.ts',
      ignore: ['h'], // 解决h报错
      imports: [
        'vue',
        {
          'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
        },
      ],
    }),
    Components({
      dts: 'src/typings/components.d.ts',
      dirs: ['src/components'],
      types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
      resolvers: [NaiveUiResolver()],
    }),
    unocss(),
    visualizer({
      gzipSize: true,
      brotliSize: true,
      emitFile: false,
      filename: 'dist/stats.html', // 分析图生成的文件名
      open: true, // 如果存在本地服务端口，将在打包后自动展示
    }),
    phpPlugin(isBuild),
    // viteCompression({
    //   algorithm: "gzip", // 使用 gzip 压缩
    //   threshold: 1024000 // 对大于 1mb 的文件进行压缩
    // }),
    // viteCompression({
    //   algorithm: "brotliCompress", // 使用 gzip 压缩
    //   threshold: 1024000 // 对大于 1mb 的文件进行压缩
    // })
  ])
  if (isBuild) {
    // @ts-ignore
    plugins.push(setupLaravel(configEnv))
    plugins.push(
      createExternal({
        interop: 'auto',
        externals: {
          vue: 'Vue',
          'naive-ui': 'naive',
          '@na/utils': 'VueUtil',
          '@na/echarts': 'VueChart',
          '@na/ckeditor': 'VueCkeditor',
          '@na/sfc-loader': 'Vue3SfcRender',
        },
      }),
    )
    plugins.push(
      cp({
        targets: [
          copyFile('vue/dist/vue.global.prod.js', 'vue.min.js'),
          copyFile('naive-ui/dist/index.prod.js', 'naive-ui.min.js'),
          copyFile('@na/utils/dist/umd/vue-util.umd.js', 'vue-util.min.js'),
          // copyFile('@na/echarts/dist/umd/vue-chart.umd.js','vue-chart.min.js'),
          copyFile('@na/components/node_modules/@na/echarts/dist/umd/vue-chart.umd.js', 'vue-chart.min.js'),
          copyFile('@na/components/node_modules/@na/ckeditor/dist/umd/vue-ckeditor.umd.js', 'vue-ckeditor.min.js'),
          copyFile('@na/components/node_modules/@na/sfc-loader/dist/umd/vue3-sfc-loader.umd.js', 'vue3-sfc-loader.min.js'),
          // copyFile('@na/components/node_modules/cropperjs/dist/cropper.min.js'),
        ],
      }),
    )
  }
  return plugins
}
